package com.winning.java.reflection;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.Console;
import java.nio.channels.InterruptedByTimeoutException;

/**
 * @author: rqx
 * @create: 2023/6/5 10:02
 * @description:
 */
@Slf4j
public class Main {
    @SneakyThrows
    @Test
    public void ReflectionTest() {
        var type = Class.forName("com.winning.java.reflection.People");
        log.info("type:{}", type);

        var constructor = type.getConstructor(String.class, int.class);
        var obj = constructor.newInstance("MyName", 100);

        var method = type.getMethod("toString");
        var msg1 = method.invoke(obj);
        log.info("obj1:{}", msg1);

        var normalFields = type.getFields();
        var fields = type.getDeclaredFields();
        log.info("normalFields.length:{}", normalFields.length);
        log.info("fields.length:{}", fields.length);

        var fname = type.getField("name");
        fname.set(obj, "MyName666");
        var fage = type.getDeclaredField("age");
        fage.setAccessible(true);
        fage.set(obj, 222);

        var msg2 = method.invoke(obj);
        log.info("obj2:{}", msg2);

        var attr1 = fname.getAnnotation(MyAtt.class);
        var attr2 = fage.getAnnotation(MyAtt.class);
        log.info("attr1:{}", attr1);
        log.info("attr1:{}", attr2.Text());
    }
}
